<?php

//图片文件的类型type:
// fs: 首页幻灯
// fd: 首页底部图片
// fp: 首页图片
// zs: 最受欢迎产品幻灯片
// zp: 最受欢迎产品图片
// jd: 经典护肤
// jdp: 经典护肤底部图片
// zx: 产品咨询


function aijia_pic_admin() {
	$output = '';

	$output .= '<p>'.l('首页图片管理', 'admin/aijia_pic_admin/sy').'</p>';
  $output .= '<p>' . l('最受欢迎图片管理', 'admin/aijia_pic_admin/zs') . '</p>';
  $output .= '<p>' . l('经典护肤图片管理', 'admin/aijia_pic_admin/jd') . '</p>';
  $output .= '<p>' . l('产品咨询图片管理', 'admin/aijia_pic_admin/zx') . '</p>';

	return $output;

}

//首页滚动图片
function aijia_front_pic_form() {
	$form = array();

	$query = db_select('aijia_pic','p')
		->fields('p')
    	->condition('p.type', 'fs')
		->orderBy('p.id', 'DESC')
  		->execute()->fetchAll();
	$row = array();
	$i = 1;
	foreach($query as $item){

		$file = file_load($item->fid);

		$row[$item ->fid] = array(
			'num' => $i,
			'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
			'url' => $item->url
		);
		$i++;
	}

	$header = array(
		'num' => t('序号'),
		'pic' => t('图片预览'),
		'url' => t('链接'),

	);
	//首页滚动图片
	$form['fp'] = array(
		'#type' => 'fieldset',
		'#collapsible' => true,
		'#title' => t('首页滚动图片')
	);

	$form['fp']['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('没有图片!'),
    	'#multiple' => true,
  	);

  	$form['fp']['submit_delete'] = array(
		'#type' => 'submit',
		'#value' => t('删除'),
		'#submit' => array('pic_delete')
  	);

  	// If this #attribute is not present, upload will fail on submit
  	$form['#attributes']['enctype'] = 'multipart/form-data';
  	$form['fp']['file'] = array(
    	'#title' => t('上传首页滚动图片'),
    	'#type'  => 'file'
  	);

  	$form['fp']['url'] = array(
    	'#title' => t('链接地址'),
    	'#type'  => 'textfield',
    	'#size' => 160
  	);

  	$form['fp']['submit_upload'] = array(
    	'#type' => 'submit',
    	'#value' => '上传',
    	'#validate' => array('pic_upload_validate'),
    	'#submit' => array('fp_pic_upload_submit')
  	);
  	return $form;
}


//首页底部图片
function aijia_front_dpic_form() {
  $form = array();

  $query = db_select('aijia_pic','p')
    ->fields('p')
    ->condition('p.type', 'fd')
    ->orderBy('p.id', 'DESC')
    ->execute()->fetchAll();
  $row = array();
  $i = 1;
  foreach($query as $item){

    $file = file_load($item->fid);

    $row[$item ->fid] = array(
      'num' => $i,
      'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
      'url' => $item->url
    );
    $i++;
  }

  $header = array(
    'num' => t('序号'),
    'pic' => t('图片预览'),
    'url' => t('链接'),

  );

  $form['fp'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#title' => t('首页底部图片'),
    '#description' => t('图片大小为234*160')
  );

  $form['fp']['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $row,
      '#empty' => t('没有图片!'),
      '#multiple' => true,
    );

    $form['fp']['submit_delete'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
    '#submit' => array('pic_delete')
    );

    // If this #attribute is not present, upload will fail on submit
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['fp']['file'] = array(
      '#title' => t('上传首页底部图片'),
      '#type'  => 'file',
      '#description' => t('图片大小为234*160')
    );

    $form['fp']['url'] = array(
      '#title' => t('链接地址'),
      '#type'  => 'textfield',
      '#size' => 160
    );

    $form['fp']['submit_upload'] = array(
      '#type' => 'submit',
      '#value' => '上传',
      '#validate' => array('pic_upload_validate'),
      '#submit' => array('fd_pic_upload_submit')
    );
    return $form;
}

//最受欢迎产品图片表单
function zshy_pic_form() {
	$form = array();

	$query = db_select('aijia_pic','p')
		->fields('p')
    	->condition('p.type', 'zp')
		->orderBy('p.id', 'DESC')
  		->execute()->fetchAll();
	$row = array();
	$i = 1;
	foreach($query as $item){

		$file = file_load($item->fid);

		$row[$item ->fid] = array(
			'num' => $i,
			'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
			'url' => $item->url
		);
		$i++;
	}

	$header = array(
		'num' => t('序号'),
		'pic' => t('图片预览'),
		'url' => t('链接'),

	);

	$form['fp'] = array(
		'#type' => 'fieldset',
		'#collapsible' => true,
		'#title' => t('最受欢迎产片图片')
	);

	$form['fp']['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('没有图片!'),
    	'#multiple' => true,
  	);

  	$form['fp']['submit_delete'] = array(
		'#type' => 'submit',
		'#value' => t('删除'),
		'#submit' => array('pic_delete')
  	);

  	// If this #attribute is not present, upload will fail on submit
  	$form['#attributes']['enctype'] = 'multipart/form-data';
  	$form['fp']['file'] = array(
    	'#title' => t('上传最受欢迎图片'),
    	'#type'  => 'file'
  	);

  	$form['fp']['url'] = array(
    	'#title' => t('链接地址'),
    	'#type'  => 'textfield',
    	'#size' => 160
  	);

  	$form['fp']['submit_upload'] = array(
    	'#type' => 'submit',
    	'#value' => '上传',
    	'#validate' => array('pic_upload_validate'),
    	'#submit' => array('zp_pic_upload_submit')
  	);
  	return $form;
}

//最受欢迎传品换幻灯片表单
function zshy_slide_pic_form() {
  $form = array();

  $query = db_select('aijia_pic','p')
    ->fields('p')
      ->condition('p.type', 'zs')
    ->orderBy('p.id', 'DESC')
      ->execute()->fetchAll();
  $row = array();
  $i = 1;
  foreach($query as $item){

    $file = file_load($item->fid);

    $row[$item ->fid] = array(
      'num' => $i,
      'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
      'url' => $item->url
    );
    $i++;
  }

  $header = array(
    'num' => t('序号'),
    'pic' => t('图片预览'),
    'url' => t('链接'),

  );

  $form['fp'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#title' => t('最受欢迎产片幻灯图片')
  );

  $form['fp']['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $row,
      '#empty' => t('没有图片!'),
      '#multiple' => true,
    );

    $form['fp']['submit_delete'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
    '#submit' => array('pic_delete')
    );

    // If this #attribute is not present, upload will fail on submit
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['fp']['file'] = array(
      '#title' => t('上传最受欢迎滚动图片'),
      '#type'  => 'file'
    );

    $form['fp']['url'] = array(
      '#title' => t('链接地址'),
      '#type'  => 'textfield',
      '#size' => 160
    );

    $form['fp']['submit_upload'] = array(
      '#type' => 'submit',
      '#value' => '上传',
      '#validate' => array('pic_upload_validate'),
      '#submit' => array('zs_pic_upload_submit')
    );
    return $form;
}

//经典护肤幻灯表单
function jdhf_slide_pic_form() {
	$form = array();

	$query = db_select('aijia_pic','p')
		->fields('p')
    	->condition('p.type', 'jd')
		->orderBy('p.id', 'DESC')
  		->execute()->fetchAll();
	$row = array();
	$i = 1;
	foreach($query as $item){

		$file = file_load($item->fid);

		$row[$item ->fid] = array(
			'num' => $i,
			'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
			'url' => $item->url
		);
		$i++;
	}

	$header = array(
		'num' => t('序号'),
		'pic' => t('图片预览'),
		'url' => t('链接'),

	);
	//首页滚动图片
	$form['fp'] = array(
		'#type' => 'fieldset',
		'#collapsible' => true,
		'#title' => t('经典护肤幻灯片')
	);

	$form['fp']['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('没有图片!'),
    	'#multiple' => true,
  	);

  	$form['fp']['submit_delete'] = array(
		'#type' => 'submit',
		'#value' => t('删除'),
		'#submit' => array('pic_delete')
  	);

  	// If this #attribute is not present, upload will fail on submit
  	$form['#attributes']['enctype'] = 'multipart/form-data';
  	$form['fp']['file'] = array(
    	'#title' => t('上传经典护肤滚动图片'),
    	'#type'  => 'file'
  	);

  	$form['fp']['url'] = array(
    	'#title' => t('链接地址'),
    	'#type'  => 'textfield',
    	'#size' => 160
  	);

  	$form['fp']['submit_upload'] = array(
    	'#type' => 'submit',
    	'#value' => '上传',
    	'#validate' => array('pic_upload_validate'),
    	'#submit' => array('jd_pic_upload_submit')
  	);
  	return $form;
}

//经典护肤底部表单
function jdp_slide_pic_form() {
  $form = array();

  $query = db_select('aijia_pic','p')
    ->fields('p')
    ->condition('p.type', 'jdp')
    ->orderBy('p.id', 'DESC')
    ->execute()->fetchAll();
  $row = array();
  $i = 1;
  foreach($query as $item){

    $file = file_load($item->fid);

    $row[$item ->fid] = array(
      'num' => $i,
      'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
      'url' => $item->url
    );
    $i++;
  }

  $header = array(
    'num' => t('序号'),
    'pic' => t('图片预览'),
    'url' => t('链接'),

  );
  //首页滚动图片
  $form['fp'] = array(
    '#type' => 'fieldset',
    '#collapsible' => true,
    '#title' => t('经典护肤底部图片')
  );

  $form['fp']['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $row,
      '#empty' => t('没有图片!'),
      '#multiple' => true,
    );

  $form['fp']['submit_delete'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
    '#submit' => array('pic_delete')
  );

    // If this #attribute is not present, upload will fail on submit
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['fp']['file'] = array(
      '#title' => t('上传经典护肤底部图片'),
      '#type'  => 'file'
    );

    $form['fp']['url'] = array(
      '#title' => t('链接地址'),
      '#type'  => 'textfield',
      '#size' => 160
    );

    $form['fp']['submit_upload'] = array(
      '#type' => 'submit',
      '#value' => '上传',
      '#validate' => array('pic_upload_validate'),
      '#submit' => array('jdp_pic_upload_submit')
    );
    return $form;
}

//产品咨询幻灯表单
function cpzx_slide_pic_form() {
	$form = array();

	$query = db_select('aijia_pic','p')
		->fields('p')
    	->condition('p.type', 'zx')
		->orderBy('p.id', 'DESC')
  		->execute()->fetchAll();
	$row = array();
	$i = 1;
	foreach($query as $item){

		$file = file_load($item->fid);

		$row[$item ->fid] = array(
			'num' => $i,
			'pic' => '<img src="' . image_style_url('aijia_pic', $file->uri) . '" />',
			'url' => $item->url
		);
		$i++;
	}

	$header = array(
		'num' => t('序号'),
		'pic' => t('图片预览'),
		'url' => t('链接'),

	);

	$form['fp'] = array(
		'#type' => 'fieldset',
		'#collapsible' => true,
		'#title' => t('产品咨询幻灯片')
	);

	$form['fp']['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('没有图片!'),
    	'#multiple' => true,
  	);

  	$form['fp']['submit_delete'] = array(
		'#type' => 'submit',
		'#value' => t('删除'),
		'#submit' => array('pic_delete')
  	);

  	// If this #attribute is not present, upload will fail on submit
  	$form['#attributes']['enctype'] = 'multipart/form-data';
  	$form['fp']['file'] = array(
    	'#title' => t('上传产品咨询图片'),
    	'#type'  => 'file'
  	);

  	$form['fp']['url'] = array(
    	'#title' => t('链接地址'),
    	'#type'  => 'textfield',
    	'#size' => 160
  	);

  	$form['fp']['submit_upload'] = array(
    	'#type' => 'submit',
    	'#value' => '上传',
    	'#validate' => array('pic_upload_validate'),
    	'#submit' => array('zx_pic_upload_submit')
  	);
  	return $form;
}

function pic_delete($form, $form_state) {
	$fids = array_filter($form_state['values']['table']);
	foreach($fids as $value){
	  	db_delete('aijia_pic')
	  		->condition('fid', $value)
  			->execute();
  		file_delete(file_load($value));
	}
}

function pic_upload_validate($form, &$form_state) {
  	$file = file_save_upload('file', array(
    	'file_validate_is_image' => array(),
    	'file_validate_extensions' => array('png gif jpg jpeg'),
  	));
  	if ($file) {
   		if ($file = file_move($file, 'public://')) {
      		$form_state['values']['file'] = $file;
    	}else {
      		form_set_error('file', t('无法上传图片到你指定的文件夹.'));
    	}
  	}else {
    	form_set_error('file', t('上传失败.'));
  	}
}

//首页滚动提交
function fp_pic_upload_submit($form, &$form_state) {

  	$file=$form_state['values']['file'];
  	unset($form_state['values']['file']);
  	$file->status = FILE_STATUS_PERMANENT;
  	file_save($file);
  	drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
  	$url = trim($form_state['values']['url']);
  	$fields = array(
  		'fid' => $file->fid,
  		'type' => 'fs',
 		'url' => $url
	);

	db_insert('aijia_pic')->fields($fields)->execute();
}

//首页底部图片提交
function fd_pic_upload_submit($form, &$form_state) {

    $file=$form_state['values']['file'];
    unset($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
    $url = trim($form_state['values']['url']);
    $fields = array(
      'fid' => $file->fid,
      'type' => 'fd',
    'url' => $url
  );

  db_insert('aijia_pic')->fields($fields)->execute();
}

//最受欢迎产品幻灯图片提交
function zs_pic_upload_submit($form, &$form_state) {

  	$file=$form_state['values']['file'];
  	unset($form_state['values']['file']);
  	$file->status = FILE_STATUS_PERMANENT;
  	file_save($file);
  	drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
  	$url = trim($form_state['values']['url']);
  	$fields = array(
  		'fid' => $file->fid,
  		'type' => 'zs',
 		'url' => $url
	);

	db_insert('aijia_pic')->fields($fields)->execute();
}

//最受欢迎产品图片提交
function zp_pic_upload_submit($form, &$form_state) {

    $file=$form_state['values']['file'];
    unset($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
    $url = trim($form_state['values']['url']);
    $fields = array(
      'fid' => $file->fid,
      'type' => 'zp',
    'url' => $url
  );

  db_insert('aijia_pic')->fields($fields)->execute();
}

//经典护肤图片提交
function jd_pic_upload_submit($form, &$form_state) {

  	$file=$form_state['values']['file'];
  	unset($form_state['values']['file']);
  	$file->status = FILE_STATUS_PERMANENT;
  	file_save($file);
  	drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
  	$url = trim($form_state['values']['url']);
  	$fields = array(
  		'fid' => $file->fid,
  		'type' => 'jd',
 		'url' => $url
	);

	db_insert('aijia_pic')->fields($fields)->execute();
}

function jdp_pic_upload_submit($form, &$form_state) {

    $file=$form_state['values']['file'];
    unset($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
    $url = trim($form_state['values']['url']);
    $fields = array(
      'fid' => $file->fid,
      'type' => 'jdp',
    'url' => $url
  );

  db_insert('aijia_pic')->fields($fields)->execute();
}

//产品咨询图片提交
function zx_pic_upload_submit($form, &$form_state) {

  	$file=$form_state['values']['file'];
  	unset($form_state['values']['file']);
  	$file->status = FILE_STATUS_PERMANENT;
  	file_save($file);
  	drupal_set_message(t('上传成功, 文件名: @filename.', array('@filename' => $file->filename)));
  	$url = trim($form_state['values']['url']);
  	$fields = array(
  		'fid' => $file->fid,
  		'type' => 'zx',
 		'url' => $url
	);

	db_insert('aijia_pic')->fields($fields)->execute();
}

//首页图片管理
function aijia_pic_sy() {
  $output = '';

  //首页滚动图片
  $form = drupal_get_form('aijia_front_pic_form');
  $output .= drupal_render($form);

  //首页底部图片
  $form = drupal_get_form('aijia_front_dpic_form');
  $output .= drupal_render($form);

  return $output;
}

//最受欢迎产品页面图片管理
function aijia_pic_zs() {
  $output = '';

  $form = drupal_get_form('zshy_slide_pic_form');
  $output .= drupal_render($form);

  $form = drupal_get_form('zshy_pic_form');
  $output .= drupal_render($form);

  return $output;
}


//经典护肤页面
function aijia_pic_jd() {
  $output = '';

  //经典护肤幻灯片
  $form = drupal_get_form('jdhf_slide_pic_form');
  $output .= drupal_render($form);

  $form = drupal_get_form('jdp_slide_pic_form');
  $output .= drupal_render($form);
  return $output;

}

//产品咨询页面
function aijia_pic_zx() {
  $output = '';

  //产品咨询幻灯片
  $form = drupal_get_form('cpzx_slide_pic_form');
  $output .= drupal_render($form);


  return $output;

}

